package com.example.demo.controller;

import com.example.demo.dao.ProductDao;
import com.example.demo.model.Product;
import com.example.demo.service.ProductService;
import com.example.demo.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
public class uploadProductController extends BaseController{
    @Resource
    ProductService productService;
    @Resource
    ProductDao productDao;

    @PostMapping("/uploadProducts")
    public JsonResult<Void> addProduct(String types,String pname,Long price,Integer stock,String img, HttpSession session){
        Product result=productDao.findByNameAndUid(pname,getUidFromSession(session));
        if(result!=null){
                result.setStock(result.getStock()+stock);
                productDao.updateNumByPid(result.getPid(),result.getStock());
        }else {
            Product product=new Product();
            product.setTypes(types);
            product.setPname(pname);
            product.setPrice(price);
            product.setStock(stock);
            product.setImg(img);
            product.setUid(getUidFromSession(session));
            productService.addProduct(product);
        }
        return new JsonResult<>(OK);
    }


}
